<?php
namespace app\interfaces\controller;

use app\interfaces\model\GoodsType as ShopGoodsTypeModel;
use app\interfaces\model\Ad as AdModel;
use think\Request;
use think\Exception;

class GoodsType
{

    /*
     * 用户端 - 推荐分类列表
     */
    public function recommend_list()
    {

        $result = ShopGoodsTypeModel::where(['status' => '1', 'count' => '3','recommend' => 1])
            ->field('id,picUrl,title')
            ->order(['sort' => 'asc'])->select();

        $ad_find = AdModel::where(['parenTid' => '18', 'start_time' => ['lt', time()], 'end_time' => ['gt', time()]])
            ->field('picUrl,linkUrl,skip_type')
            ->find();

        return ['code' => 200, 'message' => '成功', 'data' => $result, 'ad_find' => $ad_find];

    }

    /*
     * 用户端 - 商品分类列表（左侧）
     */
    public function left_list()
    {

        $result = ShopGoodsTypeModel::where(['status' => '1', 'count' => '2'])
            ->field('id,title')
            ->order(['sort' => 'asc','id' => 'asc'])
            ->select();

        return ['code' => 200, 'message' => '成功', 'data' => $result];

    }

    /*
     * 用户端 - 商品分类列表（右侧）
     * @param  string $id 分类ID
     */
    public function right_list()
    {

        // 获取参数
        $id = Request::instance()->param('id');

        if (empty($id)) throw new Exception('not Empty');

        $result = ShopGoodsTypeModel::where(['status' => '1'])
            ->field('id,parenTid,title,picUrl')
            ->order(['sort' => 'asc'])
            ->select();

        // $ad_find = GoodsTypeModel::where(['id' => $id, 'status' => '1'])
        //     ->field('ad_img,linkUrl,skip_type')
        //     ->find();
//        $ad_find = AdModel::where(['parenTid' => '18', 'start_time' => ['lt', time()], 'end_time' => ['gt', time()]])
//            ->field('picUrl,linkUrl,skip_type')
//            ->find();

        return ['code' => 200, 'message' => '成功', 'data' => recursion_node($result, $id)];

    }


}
